<script setup>
import FirstTitle from "@comp/FirstTitle";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { onMounted } from "vue";
import { searchDataKqzy } from "@/api/search";
import { addOrUpdateKqzy } from "@/api/addOrUpdate";

const editorRef = shallowRef();
const editorRef1 = shallowRef();
const editorRef2 = shallowRef();

const toolbarConfig = {};
const editorConfig = {
  placeholder: "请输入内容...",
};
const editorConfig1 = {
  placeholder: "请输入内容...",
};
const editorConfig2 = {
  placeholder: "请输入内容...",
};

const mode = ref("default");
const mode1 = ref("default");
const mode2 = ref("default");

// 加载html
const initHtml = () => {
  searchDataKqzy({ m: "任务需求-任务依据" }).then((res) => {
    const { data } = res;
	valueHtml.value = data.find((x) => x.k === "我省防护布势")?.v || "";
	valueHtml1.value = data.find((x) => x.k === "省域内跨区支援任务")?.v || "";
	valueHtml2.value = data.find((x) => x.k === "市域内跨行政区支援任务")?.v || "";
  });
};

const handleCreated = (editor) => {
  editorRef.value = editor; // 记录 editor 实例，重要！
};
const handleCreated1 = (editor) => {
  editorRef1.value = editor; // 记录 editor 实例，重要！
};
const handleCreated2 = (editor) => {
  editorRef2.value = editor; // 记录 editor 实例，重要！
};

const valueHtml = ref(
  "建立“一个核心、两个重点、五个支撑”的整体防护布势，即建立以哈尔滨市为核心的防护区；以西南某某市"
);
const valueHtml1 = ref(
  "一般按照就近就地的原则实施支援保障任务，一级国家人防重点城市由周边城市向心支援；三级国家人防重点城市支援二级和一级城市，相邻的二级国家人防重点城市按照互为支援的原则组织实施。"
);
const valueHtml2 = ref(
  "一般按照就近就地的原则实施支援保障任务，一级国家人防重点城市由周边城市向心支援；三级国家人防重点城市支援二级和一级城市，相邻的二级国家人防重点城市按照互为支援的原则组织实施。<br/>③遇有特殊情况时，参与实施省域跨区支援。"
);

const saveProgramme = () => {
  ElMessageBox.confirm(`确定要保存吗?`, "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    customClass: "message-box",
  }).then(() => {
    addOrUpdateKqzy({
      m: "任务需求-任务依据",
      data: [
        {
          k: "我省防护布势",
          v: valueHtml.value,
        },
        {
          k: "省域内跨区支援任务",
          v: valueHtml1.value,
        },
        {
          k: "市域内跨行政区支援任务",
          v: valueHtml2.value,
        },
      ],
    })
      .then(() => {
        ElMessage.success("操作成功");
      })
      .catch(() => {});
  });
};

onMounted(() => {
  initHtml();
});

onBeforeUnmount(() => {
  const editor = editorRef.value;
  const editor1 = editorRef1.value;
  const editor2 = editorRef2.value;
  if (!editor) return;
  if (!editor1) return;
  if (!editor2) return;
  editor.destroy();
  editor1.destroy();
  editor2.destroy();
});
</script>
<template>
  <div class="">
    <FirstTitle name="我省防护布势" />
    <div style="border: 1px solid #ccc" class="top-15">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef"
        :defaultConfig="toolbarConfig"
        :mode="mode"
      />
      <Editor
        style="height: 500px; overflow-y: hidden"
        v-model="valueHtml"
        :defaultConfig="editorConfig"
        :mode="mode"
        @onCreated="handleCreated"
      />
    </div>

    <FirstTitle name="省域内跨区支援任务" class="top-15" />
    <div style="border: 1px solid #ccc" class="top-15">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef1"
        :defaultConfig="toolbarConfig"
        :mode="mode1"
      />
      <Editor
        style="height: 500px; overflow-y: hidden"
        v-model="valueHtml1"
        :defaultConfig="editorConfig1"
        :mode="mode1"
        @onCreated="handleCreated1"
      />
    </div>

    <FirstTitle name="市域内跨行政区支援任务" class="top-15" />
    <div style="border: 1px solid #ccc" class="top-15">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef2"
        :defaultConfig="toolbarConfig"
        :mode="mode2"
      />
      <Editor
        style="height: 500px; overflow-y: hidden"
        v-model="valueHtml2"
        :defaultConfig="editorConfig2"
        :mode="mode2"
        @onCreated="handleCreated2"
      />
    </div>

    <div class="flex-justify-end top-20">
      <el-button type="primary" class="save-btn" @click="saveProgramme"
        >保存</el-button
      >
    </div>
  </div>
</template>
<style scoped lang="scss"></style>